import etcd

# https://python-etcd.readthedocs.io/en/latest/

host='172.31.0.13'
port=2379

client = etcd.Client(host=host, port=port)

client.write('/nodes/n1', 1)
client.write('/nodes/n2', 2, ttl=4)

# create only
try:
    client.write('/nodes/n3', '3', prevExist=False)
except etcd.EtcdAlreadyExist:
    print('etcd already exist: n3')

# compare and swap atomically
client.write('/nodes/n3', 333, prevValue=33)
try:
    client.write('/nodes/n3', 3333, prevValue=3) # this fails
except etcd.EtcdCompareFailed:
    print('etcd compare failed')

client.write('/nodes/queue', None, dir=True)
print(client.read('/nodes/n3').value)

# recursively read a dir
r = client.read('/nodes', recursive=True, sorted=True)
for child in r.children:
    print('%s:%s' %(child.key, child.value))

try:
    client.read('/invalid/path')
except etcd.EtcdKeyNotFound:
    print('not found')